<?php

// +----------------------------------------------------------------------
// | Library for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 仓库地址 ：https://gitee.com/zoujingli/ThinkLibrary
// | github 仓库地址 ：https://github.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------

declare (strict_types=1);

namespace app\goods\model;

use think\admin\Model;
use think\model\relation\HasOne;
use think\facade\Config;

/**
 * 用户模型
 * @class ImsEweiShopMerchUser
 * @package think\admin\model
 */

class ImsEweiShopGoods extends Model
{
    protected $createTime = 'createtime';
    protected $updateTime = false;

    /**
     * 日志名称
     * @var string
     */
    protected $oplogName = '商品管理';

    /**
     * 商品管理
     * @var string
     */
    protected $oplogType = '商品管理';

    /**
     * 获取用户数据
     * @param mixed $map 数据查询规则
     * @param array $data 用户数据集合
     * @param string $field 原外连字段
     * @param string $target 关联目标字段
     * @param string $fields 关联数据字段
     * @return array
     */
    public static function items($map, string $fields = '*'): array
    {
        $data = static::mk()->where($map)->find()->toArray();
        return self::formatData($data);
    }

    public static function formatData($data=[]){
        
        return [
            'id'=>$data['id'],
            'title'=>$data['title'],
            'keyword'=>$data['keywords'],
            'specification'=>$data['subtitle'],
            'provenance'=>$data['province'],
            'logo'=>get_image_full_url($data['thumb']),
            'video'=>$data['video'],
            'isnew'=>$data['isnew'],
            'ishot'=>$data['ishot'],
            'isdiscount'=>$data['isdiscount'],
            'isrecommand'=>$data['isrecommand'],
            'issendfree'=>$data['issendfree'],
            'image'=>unserialize($data['thumb_url']),
            'content'=>$data['content'],
            'specs'=>[],
            'brand_name'=>'海通鲜冻',
            'coll_id'=>0,
            'list'=>[],
            'express_info'=>$data['subtitle'],
            'price_selling' => $data['marketprice']+$data['marketprice']*0.1,
            "price_discount"=> $data['marketprice'],
            "price_integral"=> $data['marketprice'],
            'number_stock' => $data['total'],
            'fhxx'=>'当日下单次日送达，如对配送时间有特殊要求，下单请备注',
            'shsm'=>'冷冻商品一经出售，如影响二次销售不予退换，非质量问题不予退换，因商品特性，请您收到货时当场验货，如商品存有质量问题，请保留好外包装，并提供相关商品证明照片于24小时和客服取得联系，超过24小时或非原厂包装概不退换。<br>PS：商品详情页规格仅供参考，抄码产品以实际到货重量为准（差价会予以返还），商品如存在个数，大小差异，因商品存在季节性，批次等问题，属于正常现象，不做退换。',
            'cart_number'=>0,
            'list' =>[
                [
                    "id"=> $data['id'],
                    "goods_id"=> $data['id'],
                    "goods_spec"=> "默认分组::默认规格",
                    "price_selling"=> $data['marketprice']+$data['marketprice']*0.1,
                    "price_integral"=> $data['marketprice'],
                    "number_stock"=> $data['total'],
                    "number_sales"=> $data['sales'],
                    "price_discount"=> $data['marketprice']
                ]
            ]
        ];
    }

    public static function formatDatas($data=[]){
        $newData = [];
        foreach ($data as $k => $v) {
            $newData[] = [
                "id" => $v['id'],
                "title" => $v['title'],
                "keyword" => $v['keyword'],
                "logo" => get_image_full_url($v['thumb']),
                "cate_id" => $v['cates'],
                "number_sales" => $v['sales'],
                "number_stock" => $v['total'],
                "provenance" => $v['province'],
                "delivery_location" => $v['delivery_location'],
                "price_discount" => $v['marketprice'],
                "price_selling" => $v['marketprice']+$v['marketprice']*0.1,
                "price_integral" => $v['marketprice'],
                "subtitle" => $v['subtitle']
            ];
        }

        return $newData;
    }

    /**
     * 格式化登录时间
     * @param string $value
     * @return string
     */
    public function getLoginAtAttr(string $value): string
    {
        return format_datetime($value);
    }

    /**
     * 格式化创建时间
     * @param mixed $value
     * @return string
     */
    public function getCreateAtAttr($value): string
    {
        return format_datetime($value);
    }

    public static function getGoodsStatusList(){
        return [
            '0' => '待审核',
            '1' => '出售中',
            '2' => '仓库中',
            '-1' => '回收站',
        ];
    }
}